/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.log4j.Logger;
import user.UserAction;

/**
 *
 * @author killian
 */
public class Utils {
    
    private static final Logger logger = Logger.getLogger(UserAction.class);
    
    public static String encrypt(String password, String key) {
        logger.info("------method encrypt");
        try {
            logger.info("Beginning encryption");
            Key clef = new SecretKeySpec(key.getBytes("ISO-8859-2"), "Blowfish");
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.ENCRYPT_MODE, clef);
            logger.info("End encryption");
            return new String(cipher.doFinal(password.getBytes()));
        } catch (Exception e) {
            logger.error("Error encryption password");
            logger.error(e);
            return null;
        }
    }

    public static String decrypt(String password, String key) {
        logger.info("------method decrypt");
        try {
            logger.info("Beginning decryption");
            Key clef = new SecretKeySpec(key.getBytes("ISO-8859-2"), "Blowfish");
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.DECRYPT_MODE, clef);
            logger.info("End decryption");
            return new String(cipher.doFinal(password.getBytes()));
        } catch (Exception e) {
            logger.error("Error decryption password");
            logger.error(e);
            return null;
        }
    }
}